package controller.nonMember;

import controller.movieSectionManipulation.MovieSectionSorting;
import databaseLayer.MovieSection_db;
import java.io.IOException;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.MovieSection;

public class NonMemberSearch extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String action = request.getParameter("action");

        if (action == null) {
            defaultAction(request, response);
        } else if (action.equalsIgnoreCase("startPurchaseWithoutLogin")) {
            startPurchaseWithoutLogin(request, response);
        } else if (action.equalsIgnoreCase("search")) {
            searchMovieSections(request, response);
        } else {
            defaultAction(request, response);
        }
    }

    private void searchMovieSections(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String movieName = request.getParameter("movieName");
        List<MovieSection> listMovieSection = null;
        if (movieName.equals("(all)")) {
            listMovieSection = MovieSection_db.selectAllMovieSections();
        } else {
            listMovieSection = MovieSection_db.selectMovieSectionsByMovieName(movieName);
        }
        String sortBy = request.getParameter("sortBy");
        MovieSectionSorting.getInstance().sortMovieSection(listMovieSection, sortBy);
        HttpSession session = request.getSession();
        session.setAttribute("listMovieSection", listMovieSection);
        RequestDispatcher requestDispatcher = request.getRequestDispatcher("/WEB-INF/nonMember/search.jsp");
        requestDispatcher.forward(request, response);
    }

    private void startPurchaseWithoutLogin(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession();

        List<String> listMovieName = MovieSection_db.selectAllMovieNames();
        session.setAttribute("listMovieName", listMovieName);

        String[] arrSortBy = MovieSectionSorting.allSortBys();
        session.setAttribute("arrSortBy", arrSortBy);
        RequestDispatcher requestDispatcher = request.getRequestDispatcher("/WEB-INF/nonMember/search.jsp");
        requestDispatcher.forward(request, response);
    }

    private void defaultAction(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        RequestDispatcher requestDispatcher = request.getRequestDispatcher("/WEB-INF/nonMember/askForLogin.jsp");
        requestDispatcher.forward(request, response);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
